package com.bawei.movie.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.RecyclerView
import com.bawei.common.entity.MovieVideoEntity
import com.bawei.image.ImageLoadUtil
import com.bawei.image.ImageOptions
import com.bawei.movie.databinding.ItemMovieLayoutBinding

/**
 * Author: Jason Zhang
 * Date: 2024/9/24
 * Desc: Paging数据适配器
 */
class MoviePagingDataAdapter : PagingDataAdapter<MovieVideoEntity, MoviePagingDataAdapter.MovieViewHolder>(MovieEntityComparator){

    private var itemClickListener: ItemClickListener? = null

    fun setItemClickListener(listener: ItemClickListener) {
        itemClickListener = listener
    }

    inner class MovieViewHolder(val dataBinding: ItemMovieLayoutBinding) : RecyclerView.ViewHolder(dataBinding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MovieViewHolder {
        val itemDataBinding = ItemMovieLayoutBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return MovieViewHolder(itemDataBinding)
    }

    override fun onBindViewHolder(holder: MovieViewHolder, position: Int) {
        val entity = getItem(position)
        if (entity != null){
            //加载视频主图
            ImageLoadUtil.with(holder.dataBinding.root.context)
                .setOptions(ImageOptions.Builder.isCircle(false).roundRadius(5).build())
                .loadUrl(entity.videomainimag).into(holder.dataBinding.ivMovieImg)
            holder.dataBinding.tvMovieTitle.text = entity.name
            holder.dataBinding.tvMovieDesc.text = entity.description
            holder.dataBinding.tvMovieTag.text = entity.taglist
            //设置点击事件
            holder.dataBinding.root.setOnClickListener {
                itemClickListener?.onItemChildClick(it, position, entity)
            }
        }
    }

    /**
     * 获取Item布局类型
     */
    override fun getItemViewType(position: Int): Int {
        return 0
    }

    interface ItemClickListener{
        fun onItemChildClick(v: View, position: Int, entity: MovieVideoEntity)
    }



}
